// 分类模块
import { topCategory } from '@/api/constant'
import { reqGetCategoryList } from '@/api/category'

export default {
  namespaced: true,
  state () {
    return {
      // 分类信息 ：根据常量数据来生成一个默认的顶级分类数据，不会出现空白（没数据的情况）
      list: topCategory.map(item => ({ name: item }))
    }
  },
  mutations: {
    setList (state, payload) {
      state.list = payload
    },
    // show hide 控制当前分类的二级分类隐藏显示
    show (state, id) {
      const curCategory = state.list.find(item => item.id === id)
      curCategory.open = true
    },
    hide (state, id) {
      const curCategory = state.list.find(item => item.id === id)
      curCategory.open = false
    }
  },
  actions: {
    async getList (context) {
      const { result } = await reqGetCategoryList()
      // console.log(result)
      // 添加控制隐藏二级类目标识
      result.forEach(item => {
        item.open = false
      })
      context.commit('setList', result)
    }
  }
}
